package com.vhuomai.store.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.vhuomai.store.entity.BaseStoreConfig;
import com.vhuomai.store.service.StoreConfigService;
import com.vhuomai.store.service.StoreService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tika.io.IOUtils;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author harris
 */
@Api(tags = "存储配置")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class StoreConfigController {
    
    @Autowired
    private StoreConfigService storeConfigService;
    @Autowired
    private StoreService storeService;
    
    @ApiOperation(value = "获取存储配置")
    @GetMapping(value = "/store/config", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseStoreConfig> storeConfig() {
        return ServiceResult.success(storeConfigService.find());
    }
    
    @ApiOperation(value = "更新配置")
    @PutMapping(value = "/store/config", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> addReply(@RequestBody BaseStoreConfig config) {
        storeConfigService.update(config);
        return ServiceResult.SUCCESS;
    }
    
    @ApiOperation(value = "上传", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/file/upload")
    public ServiceResult<String> upload(@RequestParam("file") MultipartFile file, @ApiIgnore Principal manager) throws IOException, MimeTypeException {
        MimeType type = MimeTypes.getDefaultMimeTypes().forName(file.getContentType());
        return ServiceResult.success(storeService.saveFile(file.getBytes(), type.getExtension()));
    }
    
    @ApiOperation(value = "本地文件获取")
    @GetMapping(value = "/file/**")
    public ResponseEntity<byte[]> load(HttpServletRequest request) {
        BaseStoreConfig config = storeConfigService.find();
        String url = config.getDomainUrl() + request.getServletPath();
        try {
            byte[] content = IOUtils.toByteArray(storeService.getContent(url));
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.IMAGE_GIF);
            header.setContentLength(content.length);
            header.setExpires(3600 * 24 * 365);
            return new ResponseEntity<>(content, header, HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND);
        }
    }
}
